#ifndef xpack_test_lang_cxx_parse_json
#define xpack_test_lang_cxx_parse_json
#pragma push_macro("xuser")
#undef  xuser
#define xuser mixc::test_lang_cxx_parse_json::inc
#include"test/helper.hpp" // 需要放第一
#include"lang/cxx/parse_json.hpp"
#include"lang/cxx.hpp"
#include"macro/xdebug.hpp"
#include"macro/xdebug_fail.hpp"
#define xjson(...)  #__VA_ARGS__
#pragma pop_macro("xuser")

namespace mixc::test_lang_cxx_parse_json{
    xtest("parse_json"){
        auto && alloc = [](uxx length) -> voidp {
            static byte buf[1024 * 32];
            xdebug(true, length);
            xdebug_fail(length > sizeof(buf));
            return buf;
        };
        // char js[] = xjson(
        //     ["table", "class:MyTable", "border:1px solid red", "background-color:yellow",
        //         ["tr",
        //             ["td", "class:MyTd", "border:1px solid black", "text:#5D28D1"],
        //             ["td", "class:MyTd", "border:1px solid black", "text:#5D28D1"]
        //         ],
        //         ["tr",
        //             ["td", "class:MyTd", "border:1px solid black", "text:#5D28D1"],
        //             ["td", "class:MyTd", "border:1px solid black", "text:#5D28D1"]
        //         ],
        //         ["tr",
        //             ["td", "class:MyTd", "border:1px solid black", "text:#5D28D1"],
        //             ["td", "class:MyTd", "border:1px solid black", "text:#5D28D1"]
        //         ]
        //     ]
        // );

        inc::c08 x = xjson();
        inc::c08 str;
        f64      real;
        auto     json = x.parse_json(alloc);
        auto     temp = inc::json<char>(json);
        auto     item = inc::json<char>(json);
        xassert_eq(0u, json->length);

        x    = xjson({});
        json = x.parse_json(alloc);
        xassert_eq(0u, json->length);

        x    = xjson([]);
        json = x.parse_json(alloc);
        xassert_eq(0u, json->length);

        x    = xjson({
            "name" : "PowerfulCat",
            "pwd" : "********",
            "age" : 25.5,
            "skill" : [
                "c/c++",
                "c#",
                "os",
                "math",
                "deep thinking"
            ],
            "circumference_iii" : [
                106,
                73,
                112
            ],
            "home" : "earth",
            "crop" : "iv.galax.solar_system.earth",
            "advance_mvp" : true
        });

        json  = x.parse_json(alloc);
        str   = temp = json["name"];
        xassert_eq(str, "PowerfulCat");
        xassert_eq(inc::json_type_t::jstring, temp->type);

        str   = temp = json["pwd"];
        xassert_eq(str, "********");
        xassert_eq(inc::json_type_t::jstring, temp->type);

        real  = temp = json["age"];
        xassert_eq(25.5, real);
        xassert_eq(inc::json_type_t::jfloat, temp->type);

        temp  = json["skill"];
        xassert_eq(inc::json_type_t::jarray, temp->type);
        xassert_eq(5u, temp->length);

        item  = temp[0];
        str   = item;
        xassert_eq(str, "c/c++");

        item  = temp[1];
        str   = item;
        xassert_eq(str, "c#");

        item  = temp[2];
        str   = item;
        xassert_eq(str, "os");

        item  = temp[3];
        str   = item;
        xassert_eq(str, "math");

        item  = temp[4];
        str   = item;
        xassert_eq(str, "deep thinking");

        x     = xjson({
            "import" : [
                "mixc/style.css",
                "mixc/action.js"
            ],
            "div" : {
                "class" : ["box", "red", 1 ],
                "size" : [ "100%", "20px" ],
                "empty" : [],
                "empty2" : {},
                "backcolor" : "skyblue",
                "a" : { "text":"github", "url":"https://github.com/" }
            },

            "h1" : { "class" : "red", "text" : "json is good choice !" },
            "p" : [ "hello", { "b" : "world" }, "666" ],
            "deimport" : [
                "mixc/action.js"
            ]
        });

        json  = x.parse_json(alloc);
        xassert_eq(inc::json_parse_result_t::success, json->parse_result);

        xassert_eq(inc::json_type_t::jstring, json["import"][0]->type);
        xassert_eq(inc::json_type_t::jstring, json["import"][1]->type);
        xassert_eq(inc::json_type_t::jstring, json["div"]["class"][0]->type);
        xassert_eq(inc::json_type_t::jstring, json["div"]["class"][1]->type);
        xassert_eq(inc::json_type_t::jinteger, json["div"]["class"][2]->type);
        xassert_eq(inc::json_type_t::jarray, json["div"]["empty"]->type);
        xassert_eq(inc::json_type_t::jobject, json["div"]["empty2"]->type);
        xassert_eq(inc::json_type_t::jstring, json["div"]["backcolor"]->type);
        xassert_eq(inc::json_type_t::jstring, json["div"]["a"]["text"]->type);
        xassert_eq(inc::json_type_t::jstring, json["div"]["a"]["url"]->type);
        xassert_eq(inc::json_type_t::jobject, json["div"]["a"]->type);

        xassert_eq("mixc/style.css", json["import"][0].to_cxx());
        xassert_eq("mixc/action.js", json["import"][1].to_cxx());
        xassert_eq("box", json["div"]["class"][0].to_cxx());
        xassert_eq("red", json["div"]["class"][1].to_cxx());
        xassert_eq("100%", json["div"]["size"][0].to_cxx());
        xassert_eq("20px", json["div"]["size"][1].to_cxx());
        xassert_eq("skyblue", json["div"]["backcolor"].to_cxx());
        xassert_eq("github", json["div"]["a"]["text"].to_cxx());
        xassert_eq("https://github.com/", json["div"]["a"]["url"].to_cxx());

        xassert_eq(3u, json["div"]["class"]->length);
        xassert_eq(1u, uxx(json["div"]["class"][2]));
        xassert_eq(0u, json["div"]["empty"]->length);
        xassert_eq(0u, json["div"]["empty2"]->length);
        xassert_eq(2u, json["div"]["a"]->length);
    };
}

#endif

